package com.xkdx.guangguang.pushservice;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.telephony.TelephonyManager;

import com.xkdx.caipiao.R;
import com.xkdx.guangguang.HomeActivity;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.pushservice.PushServiceModule.PushInfo;

public class PushService extends Service {
	private PushServiceAction action;
	private PushServiceModule module;
	private PushServicePresistence presistence;
	private TelephonyManager mTelephonyManager;
	private Timer mTimer;
	private static int HELLO_ID = 10;
	private Notification notification;
	private NotificationManager mNotificationManager;
	private PushInfo pushInfo;

	@Override
	public void onCreate() {
		super.onCreate();

		mTelephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

		mTimer = new Timer();
		mTimer.schedule(new TimerTask() {

			@Override
			public void run() {
				if (isInTimePeriod()) {
					mHandler.sendEmptyMessage(3);
				}
			}

		}, 10000, 60 * 60 * 1000);

	}

	Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == 3) {
				getPushInfo();
			}
		};
	};

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	public class LocalBinder extends Binder {
		// 返回本地服务
		PushService getService() {
			return PushService.this;
		}
	}

	private void getPushInfo() {

		action = new PushServiceAction(mTelephonyManager.getDeviceId());
		module = new PushServiceModule();
		presistence = new PushServicePresistence(this);
		presistence.setActitons(action);
		presistence.setModule(module);
		presistence.execute();
	}

	void showPush(HashMap<String, AbsModule> map) {
		if (map.containsKey("0")) {
			pushInfo = module.pushInfo;
			if (pushInfo.getStatus().equals("Success")) {
				IConstants.isHasPush = true;
				setNotification();
				mNotificationManager.notify(HELLO_ID, notification);
			}

		}

	}

	private boolean isInTimePeriod() {

		Calendar cal = Calendar.getInstance();// 当前日期
		int hour = cal.get(Calendar.HOUR_OF_DAY);// 获取小时
		int minute = cal.get(Calendar.MINUTE);// 获取分钟
		int minuteOfDay = hour * 60 + minute;// 从0:00分开是到目前为止的分钟数
		final int start = 10 * 60;// 起始时间 10:00的分钟数
		final int end = 21 * 60;// 结束时间 19:00的分钟数
		if (minuteOfDay >= start && minuteOfDay <= end) {
			return true;
		}

		return false;
	}

	private void setNotification() {
		String ns = Context.NOTIFICATION_SERVICE;
		mNotificationManager = (NotificationManager) getSystemService(ns);
		int icon = R.drawable.main;
		CharSequence tickerText = getString(R.string.push_name);
		long longwhen = System.currentTimeMillis();

		notification = new Notification(icon, tickerText, longwhen);
		Intent intent = new Intent(this, HomeActivity.class);
		intent.putExtra("push", "");
		IConstants.objectType = pushInfo.getObjectType();
		IConstants.objectValue = pushInfo.getObjectValue();
		PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
		notification.flags = Notification.FLAG_AUTO_CANCEL;
		notification.setLatestEventInfo(this, getString(R.string.push_notify),
				pushInfo.getMessage(), pi);
	}

}
